﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Englishwordlist
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        OpenFileDialog openFileDialog2 = new OpenFileDialog();
        ArrayList wordlist = new ArrayList();
        ArrayList letterlist = new ArrayList();
        /// <summary>
        /// 初始化字符列表，包含连词符号
        /// </summary>
        /// <param name="ll"></param>
        private void Initletterlist(ArrayList ll)
        {
            for(int i = 65; i < 91; i++)
            {
                string alpha = ((char)i).ToString();
                ll.Add(alpha);
            }
            for (int i = 97; i < 123; i++)
            {
                string alpha = ((char)i).ToString();
                ll.Add(alpha);
            }
            ll.Add("-");
            ll.Add("'");
        }
        private void BtnChooseInputFile_Click(object sender, EventArgs e)
        {
            //openFileDialog1.Filter = "文档(*.doc;*.docx)|*.doc;*.docx";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialog1.FileName != "")
                {
                    this.textBox1.Text = openFileDialog1.FileName;
                }
            }

        }

        private void BtnChooseOutputFile_Click(object sender, EventArgs e)
        {
            //openFileDialog1.Filter = "文档(*.doc;*.docx)|*.doc;*.docx";
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialog2.FileName != "")
                {
                    this.textBox2.Text = openFileDialog2.FileName;
                }
            }
            //todo:如果只是选择路径，而没有选择文件名，且目标路径没有对应输出的文件，
            //则自动生成目标文件
        }
        /// <summary>
        /// 判读某个字符是否在预设的不断词数组内,如果遇到‘等连词符号，则不断词
        
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        private bool Notbreakword(string t)
        {
            bool flag = false;
           foreach(string s in letterlist)
            {
                if(s==t)
                {
                    flag = true;
                    break;
                }
            }           
            return flag;
        }
        private void Debugtext(string str)
        {
            this.textBoxdebug.Text += str;
            this.textBoxdebug.Text += "\r\n";
        }


        private void BtnChange_Click(object sender, EventArgs e)
        {
            //判读是否正确设置读取写入的文件
            if (this.textBox1.Text != "" && this.textBox2.Text != "")
            {
                //读取目标文本
                StreamReader sR = File.OpenText(this.textBox1.Text);
                string nextLine;
                string word = "";
                ArrayList words = new ArrayList();
                //逐行读取文本
                while ((nextLine = sR.ReadLine()) != null)
                {      
                    //段落转化为单词，遇到空格标点等断词，并抛弃标点符号。
                     for (int i = 0; i < nextLine.Length; i++)
                    {
                        string letter = nextLine.Substring(i, 1);
                        if (Notbreakword(letter))
                        {
                            word += letter;
                        }
                        else
                        {
                            //this.textBoxdebug.Text += "    ";
                            //this.textBoxdebug.Text += word;
                            wordlist.Add(word);
                            word = "";
                        }                        
                    }             
                }
                sR.Close();
                //执行到此处，wordlist已经获取了全部单词，包含一些空行。
                //todo:读取目标文本，合并词汇列表，排序，去重，写入目标
            }
            else
            {
                MessageBox.Show("请正确选择文件");
            }
            
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
            int intAsciiCode = (int)asciiEncoding.GetBytes("?")[0];
            //MessageBox.Show(intAsciiCode.ToString());
            //MessageBox.Show(breakword("?").ToString());
            //foreach(string s in wordlist)
            //{
            //    Debugtext(s);
            //    Debugtext(s.Length.ToString());
            //}
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Initletterlist(letterlist);
        }
    }
}
